package kq

import (
	"context"
	"encoding/json"
	"github.com/zeromicro/go-zero/core/logx"
	"navi-mumbai/app/mq/usercenter/internal/svc"
	"navi-mumbai/app/rpc/stat/pb"
	"navi-mumbai/common/kqmsg"
)

/*
*
Listening to the payment flow status change notification message queue
*/
type AddUserLoginLogMq struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddUserLoginLogMq(ctx context.Context, svcCtx *svc.ServiceContext) *AddUserLoginLogMq {
	return &AddUserLoginLogMq{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddUserLoginLogMq) Consume(_, val string) error {
	var message kqmsg.KqAddUserLoginLogMsg
	err := json.Unmarshal([]byte(val), &message)
	if err != nil {
		l.Logger.Errorf("AddUserLoginLogMq->Consume Unmarshal err : %+v , val: %s", err, val)
		return err
	}

	err = l.execService(&message)
	if err != nil {
		l.Logger.Errorf("AddUserLoginLogMq->execService err : %+v , val :%s , message:%+v", err, val, message)
		return err
	}
	return nil
}

func (l *AddUserLoginLogMq) execService(msg *kqmsg.KqAddUserLoginLogMsg) (err error) {
	var in pb.AddUserLoginLogReq
	in.Uid = msg.Uid
	in.UserChannel = msg.UserChannel
	in.LoginTime = msg.LoginTime
	_, err = l.svcCtx.StatSysRpc.AddUserLoginLog(l.ctx, &in)
	if err != nil {
		return err
	}
	return nil
}
